package cn.cvs.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Package: cn.cvs.utils
 * @ClassName: ConfigManager
 * @Author: 862982795
 * @CreateTime: 2021/07/15 9:24
 * @Description:通过单例模式读取配置文件
 */
public class ConfigManager {
    private static Properties properties;
    // 在类加载时即创建 ConfigManager 实例
    private static ConfigManager configManager=new ConfigManager();

    // 杜绝外界创建该类对象，私有构造方法
    private ConfigManager(){
        String configFile="database.properties";
        properties=new Properties();
        InputStream is=ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
        try {
            properties.load(is);
            is.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    // 对外界提供的获取 ConfigManager 实例的方法
    public static synchronized ConfigManager getInstance(){
        if (configManager != null) {
            configManager=new ConfigManager();
        }
        return configManager;
    }

    // 饿汉模式
    public static ConfigManager ConfigManagerGetInstance(){
        return configManager;
    }

    // 读取配置文件信息
    public String getValue(String key){
        return properties.getProperty(key);
    }
}
